package com.greendragon.persistence.dao.impl; 

import java.util.List;
import org.apache.log4j.Logger;
import com.greendragon.persistence.dao.ICommentDao;
import com.greendragon.persistence.domain.PageComment;
import com.greendragon.persistence.enmu.FetchOrderType;
import com.greendragon.persistence.enmu.LockType;
import com.greendragon.persistence.enmu.VerifyType;

/** 
 * @author wulibin, exceptsky@gmail.com, 2011-12-6 下午06:55:50
 * 
 */
@SuppressWarnings("unchecked")
public class PageCommentDaoImpl extends BaseDao<PageComment> implements ICommentDao<PageComment>{
	private static Logger          logger           = Logger.getLogger(PageCommentDaoImpl.class);
	
	@Override
	public long getCountOfObj(long pid,  LockType lockType, VerifyType verifyType) {
		String hql = "select count(pc.id) from PageComment as pc where pc.page.id = ? and"+getLockSql("pc",lockType);
		try {
			List rs = getHibernateTemplate().find(hql, pid);
			if(rs==null || rs.size()==0){
				return 0l;
			}
			long count = (Long)rs.listIterator().next();
			return count;
		} catch (Exception e) {
			logger.error(e);
			return 0l;
		}
	}

	@Override
	public long getCountOfUser(long uid, LockType lockType, VerifyType verifyType) {
		String hql = "select count(pc.id) from PageComment as pc where pc.page.id = ? and"+getLockSql("pc",lockType);
		try {
			List rs = getHibernateTemplate().find(hql, uid);
			if(rs==null || rs.size()==0){
				return 0l;
			}
			long count = (Long)rs.listIterator().next();
			return count;
		} catch (Exception e) {
			logger.error(e);
			return 0l;
		}
	}

	@Override
	public List<PageComment> getOfObj(long pid, int offset, int length, LockType lockType, VerifyType verifyType, FetchOrderType orderType) {
		String hql = "from PageComment as pc where pc.page.id = ? and";
		hql += getLockSql("pc",lockType);
		hql += getOrderSql("pc", orderType);
		return selectByPage(hql, new Long[]{new Long(pid)}, offset, length);
	}
}
 